package com.ruoyi.machine.advanced.alarm.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.machine.advanced.alarm.domain.MachineAlarm;
import com.ruoyi.machine.advanced.alarm.domain.MachineAlarmCond;
import com.ruoyi.machine.advanced.alarm.service.IMachineAlarmService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 设备警报
 */
@RestController
@RequestMapping("/machine/alarm")
@Api(value = "设备警报模块", tags = "设备警报接口")
public class MachineAlarmController extends BaseController {
    @Autowired
    private IMachineAlarmService machineAlarmService;

    /**
     * 获取设备警报
     * @param cond
     * @return
     */
    @ApiOperation(value = "获取设备警报列表",notes = "查询所有设备警报信息")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody MachineAlarmCond cond) {
        startPage();
        List<MachineAlarm> machineAlarmList = machineAlarmService.selectMachineAlarmList(cond);
        return getDataTable(machineAlarmList);
    }

    /**
     * 导出设备警报
     *
     * @param cond
     * @return
     */
    @PostMapping("/export")
    public AjaxResult export(@RequestBody MachineAlarmCond cond) {
        List<MachineAlarm> machineAlarmList = machineAlarmService.selectMachineAlarmList(cond);
        ExcelUtil<MachineAlarm> util = new ExcelUtil<>(MachineAlarm.class);
        return util.exportExcel(machineAlarmList, "设备警报信息");
    }

    /**
     * 新增设备警报
     * @param machineAlarm
     * @return
     */
    @ApiOperation(value = "新增设备警报", notes = "新增设备警报信息")
    @ApiImplicitParam(name = "machineAlarm", value = "新增设备警报", dataType = "MachineAlarm")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody MachineAlarm machineAlarm) {
        try {
            machineAlarmService.insertMachineAlarm(machineAlarm);
            return AjaxResult.success("新增成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("新增失败");
        }
    }
}
